//
//  KDChooseTableViewCell.m
//  LaiKeDaoClient
//
//  Created by dongkedi的iMac on 15/11/12.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "KDChooseTableViewCell.h"

@implementation KDChooseTableViewCell

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"0xF4F4F4"];
        
        
    }
    
    return self;
}

- (void)notificationXiangMu:(NSNotification *)noti
{
    if (noti.object == self) {
        //
    }else{
        _btn1.selected = NO;
    }
}

- (void)notificationFangAn:(NSNotification *)noti
{
    if (noti.object == self) {
        //
    }else{
        _btn1.selected = NO;
    }
}

- (void)reloadCell:(NSIndexPath *)indexPath name:(NSString *)name
{
    self.indexPath = indexPath;
    if (indexPath.section == 1) {
        //按性别
        [self.btn1 setFrame:(CGRect){16,10,16*2,16*2}];
//        [self.btn1 setSelected:NO];
        UILabel *man =(UILabel *)[self viewWithTag:1000];
        if (!man) {
            man = [[UILabel alloc] initWithFrame:(CGRect){X(_btn1)+WIDTH(_btn1)+8,Y(_btn1),14,14}];
            [man setTextAlignment:NSTextAlignmentLeft];
            [man setFont:[UIFont systemFontOfSize:13]];
            [man setBackgroundColor:[UIColor clearColor]];
            [man setText:@"男"];
            [man setTag:1000];
            [self addSubview:man];
        }
        
        [self.btn2 setFrame:(CGRect){X(man)+WIDTH(man)+42,Y(_btn1),WIDTH(_btn1),HEIGHT(_btn1)}];
//        [self.btn2 setSelected:NO];
        UILabel *woman;
        if (![self viewWithTag:2000]) {
            woman = [[UILabel alloc] initWithFrame:(CGRect){X(_btn2)+WIDTH(_btn2)+8,Y(_btn1),14,14}];
            [woman setTextAlignment:NSTextAlignmentLeft];
            [woman setFont:[UIFont systemFontOfSize:13]];
            [woman setBackgroundColor:[UIColor clearColor]];
            [woman setText:@"女"];
            [self addSubview:woman];
            [woman setTag:2000];
        }
       
    }else if (indexPath.section == 2){
        //按年龄
        [self.textField setTextColor:[UIColor colorWithHexString:@"aaaaaa"]];
    }else{
        self.nameLabel.text = name;
        [self.btn1 setFrame:(CGRect){kScreenWidth-16-11,10,16*2,16*2}];
        if (self.indexPath.section == 3) {
           
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationXiangMu:) name:kNotificationXiangMu object:nil];
        }else if (self.indexPath.section == 4){
            
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationFangAn:) name:kNotificationFangAn object:nil];
        }
    }
}

- (UIButton *)btn1{
    if (!_btn1) {
        _btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_btn1 setImage:[UIImage imageNamed:@"select@2X.png"] forState:UIControlStateNormal];
        [_btn1 setImage:[UIImage imageNamed:@"select_pre@2X.png"] forState:UIControlStateSelected];
        _btn1.imageEdgeInsets = UIEdgeInsetsMake(0,0,16,16);
        [self addSubview:_btn1];
    }
    return _btn1;
}

- (UIButton *)btn2{
    if (!_btn2) {
        _btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn2 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_btn2 setImage:[UIImage imageNamed:@"select@2X.png"] forState:UIControlStateNormal];
        [_btn2 setImage:[UIImage imageNamed:@"select_pre@2X.png"] forState:UIControlStateSelected];
        _btn2.imageEdgeInsets = UIEdgeInsetsMake(0,0,16,16);
        [self addSubview:_btn2];
    }
    return _btn2;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:(CGRect){17,12,200,14}];
        [_nameLabel setTextAlignment:NSTextAlignmentLeft];
        [_nameLabel setFont:[UIFont systemFontOfSize:13]];
        [_nameLabel setBackgroundColor:[UIColor clearColor]];
        [_nameLabel setText:@"标题"];
        [self addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UITextField *)textField{
    if (!_textField) {
        _textField = [[UITextField alloc] initWithFrame:(CGRect){16,5,59,23}];
        [_textField setBorderStyle:UITextBorderStyleBezel];
        [_textField setTextAlignment:NSTextAlignmentLeft];
        [_textField setPlaceholder:@"年龄"];
        [_textField setKeyboardType:UIKeyboardTypeNumberPad];
        [self addSubview:_textField];
        _textField.delegate = self;
    }
    return _textField;
}

//- (void)hidenKey
//{
//    [_textField resignFirstResponder];
//}
//
//- (BOOL)textFieldShouldReturn:(UITextField *)textField
//{
//    [self hidenKey];
//    return YES;
//}

- (void)buttonClick:(UIButton *)btn
{
    if (self.indexPath.section == 1) {
        if (btn == _btn1) {
            if (_btn1.selected) {
                _btn1.selected = NO;
                _btn2.selected = YES;
            }else{
                _btn2.selected = NO;
                _btn1.selected = YES;
            }
        }else if (btn == _btn2){
            if (_btn2.selected) {
                _btn2.selected = NO;
                _btn1.selected = YES;
            }else{
                _btn1.selected = NO;
                _btn2.selected = YES;
            }
        }
    }else if (self.indexPath.section == 2){
        
    }else{
        if (_btn1) {
            _btn1.selected = !_btn1.selected;
            if (self.indexPath.section == 3) {
                 [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationXiangMu object:self];
            }else if (self.indexPath.section == 4){
                [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationFangAn object:self];
            }
        }
    }
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
